<!DOCTYPE html>
<html lang="en">

<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewthport" content="width=device-width, initial-scale=1.0">
   <title>Document</title>
</head>

<body>
   <div id="app">
      <table>
         <tr>
            <th><input type="checkbox" name="" id="" v-model="toggleAll"></th>
            <th>编号</th>
            <th>名称</th>
            <th>价格</th>
            <th>数量</th>
            <th>小计</th>
            <th>操作</th>
         </tr>
         <tr v-for="(item,index) in list" :key="item.id">
            <td><input type="checkbox" v-model="item.is_finish"></td>
            <td>{{item.id}}</td>
            <td>{{item.name}}</td>
            <td>{{item.price}}</td>
            <td>
               <button :disabled="item.num<=1 ? true : false" @click="decrease(index)">-</button>
               <span>{{item.num}}</span>
               <button @click="add(index)">+</button>
            </td>
            <td>{{item.price * item.num}}</td>
            <td><button @click="del(index)">删除</button></td>
         </tr>
      </table>
      <div>
         <span>商品数量:{{shoppingNum}},</span>
         <span>合计:¥{{totalPrice}}</span>
      </div>
   </div>
</body>

</html>
<script src="./lib/vue.js"></script>
<script>
   const vm = new Vue({
      el: '#app',
      data: {
         list: [
            {
               id: 01,
               name: '铅笔',
               price: 1.5,
               num: 1,
               is_finish: false
            },
            {
               id: 02,
               name: '钢笔',
               price: 45,
               num: 1,
               is_finish: false
            },
            {
               id: 03,
               name: '签字笔',
               price: 2.5,
               num: 3,
               is_finish: false
            },
            {
               id: 04,
               name: '毛笔',
               price: 35,
               num: 2,
               is_finish: false
            }
         ],

         checkAll: '' //全选
      },
      computed: {
         // 商品数量:
         shoppingNum() {
            return this.list.reduce(function (numValue, list) {
               return numValue + list.num
            }, 0);
         },
         // 总价
         totalPrice() {
            return this.list.reduce(function (preValue, list) {
               return preValue + list.price * list.num
            }, 0)
         },
         // 全选 
         toggleAll: {
            get() {
               return this.list.every(item => item.is_finish)
            },
            set(val) {
               // 修改 toggleAll的值
               this.list.forEach(item => item.is_finish = val)
            }
         }
      },
      methods: {
         // 删除按钮
         del(index) {
            this.list.splice(index, 1)
         },
         // 减少按钮
         decrease(index) {
            this.list[index].num--
         },
         // 增加按钮
         add(index) {
            this.list[index].num++
         }
      }
   })
</script>